﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace GluePathModule
{
    
    public class GluePathCenter
    {
        public List<GluePathGroup> listGluePathGroup;
        public GluePathCenter()
        {
            listGluePathGroup = new List<GluePathGroup>();
        }
        public static GluePathCenter Load(bool bGlobal)
        {
            string strFileName;
            string strFilePath;
           
            strFilePath = ProjectModule.ProjectManager.ProjectPath + "\\" + @"GluePathSetting\";
            strFileName = strFilePath  + "ContinuePathCenter" + ".xml";

            GluePathCenter pDoc = null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(GluePathCenter));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(strFileName);
                pDoc = (GluePathCenter)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
            }
            catch (IOException eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new GluePathCenter();
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                MessageBox.Show(eMy.ToString() + "即将打开备份文件", strFileName);

                fsReader = File.OpenRead(strFileName + ".bak");
                pDoc = (GluePathCenter)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.Save(true);
            }
            return pDoc;
        }
        public bool Save(bool bGlobal)
        {
            string strFileName;
            string strFilePath;
            
            strFilePath = ProjectModule.ProjectManager.ProjectPath + "\\" + @"GluePathSetting\\";
            strFileName = strFilePath + "ContinuePathCenter" + ".xml";
            
           
            if (!Directory.Exists(strFilePath))
            {
                Directory.CreateDirectory(strFilePath);
            }
           

            FileStream fsWriter = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(GluePathCenter));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(strFileName, strFileName + ".bak", true);

            return true;
        }
       
    }
}
